package com.office.supplies.service.impl;

import com.office.supplies.mapper.BuyMapper;
import com.office.supplies.mapper.SaleMapper;
import com.office.supplies.mapper.StatisticsMapper;
import com.office.supplies.mapper.StockMapper;
import com.office.supplies.pojo.vo.CountVO;
import com.office.supplies.pojo.vo.SaleStatisticsVO;
import com.office.supplies.pojo.vo.SalesProportionOfGoodsVO;
import com.office.supplies.pojo.vo.TurnoverVO;
import com.office.supplies.service.StatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author admin
 * @Description TODO
 * @Date 2023/12/13/9:57
 */
@Service
@Slf4j
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Autowired
    private StockMapper stockMapper;

    @Autowired
    private BuyMapper buyMapper;

    @Autowired
    private SaleMapper saleMapper;

    /**
     * 营业额统计
     *
     * @param begin Date
     * @param end   Date
     * @return TurnoverVO
     */
    public TurnoverVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        //日期列表（从begin到end有那几天:{2022-10-01,2022-10-02,2022-10-03}）
        ArrayList<LocalDate> dateList = new ArrayList<>();

        dateList.add(begin);

        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        // 获取每天的营业额
        //营业额406.0,1520.0,75.0
        ArrayList<Double> statisticsList = new ArrayList<>();

        //select * from order where order_time > (这一天的00:00:00) and order_time (这一天的23:59:59)  and status = 5
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map<String, Object> map = new HashMap<>();
            map.put("beginTime", beginTime);
            map.put("endTime", endTime);
            Double totalPrice = statisticsMapper.sumByMap(map);
            totalPrice = totalPrice == null ? 0.0 : totalPrice;
            statisticsList.add(totalPrice);
        }

        return TurnoverVO.builder()
                .turnoversList(statisticsList)
                .dateList(dateList)
                .build();
    }

    /**
     * 总体销售占比
     *
     * @return SalesProportionOfGoodsVO
     */
    public List<SalesProportionOfGoodsVO> getSalesProportionOfGoods() {

        // 获取全部商品id（销售表中）
        List<Long> saleGoodsIds = saleMapper.getSaleGoodsIds();

        // 每个商品对应的销售信息和商品信息
        List<SalesProportionOfGoodsVO> ofGoodsVOList = new ArrayList<>();

        for (Long goodsId : saleGoodsIds) {
            Map<String, Integer> map = saleMapper.getGoodsNameAndSaleCountByGoodsId(goodsId);
            SalesProportionOfGoodsVO ofGoodsVO = SalesProportionOfGoodsVO.builder()
                    .goodsName(String.valueOf(map.get("goodsName")))
                    .saleCount(Integer.parseInt(String.valueOf(map.get("saleCount"))))
                    .build();
            ofGoodsVOList.add(ofGoodsVO);

        }

        return ofGoodsVOList;
    }

    /**
     * 获取库存数，销售订单数，采购订单数
     *
     * @return CountVO
     */
    public CountVO getAllCount() {

        Integer stockCount = stockMapper.getStockCount();

        Integer buyOrderCount = buyMapper.getBuyOrderCount();

        Integer saleOrderCount = saleMapper.getSaleOrderCount();

        return CountVO.builder()
                .stockCount(stockCount)
                .buyOrderCount(buyOrderCount)
                .saleOrderCount(saleOrderCount)
                .build();
    }

    @Override
    public SaleStatisticsVO getSaleStatistics(LocalDate begin, LocalDate end) {
        //日期列表（从begin到end有那几天:{2022-10-01,2022-10-02,2022-10-03}）
        ArrayList<LocalDate> dateList = new ArrayList<>();

        dateList.add(begin);

        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        // 获取每天的营业额
        //营业额406.0,1520.0,75.0
        ArrayList<Integer> statisticsList = new ArrayList<>();

        //select * from order where order_time > (这一天的00:00:00) and order_time (这一天的23:59:59)  and status = 5
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map<String, Object> map = new HashMap<>();
            map.put("beginTime", beginTime);
            map.put("endTime", endTime);
            Integer totalCount = statisticsMapper.sumByMapFormSale(map);
            totalCount = totalCount == null ? 0 : totalCount;
            statisticsList.add(totalCount);
        }

        return SaleStatisticsVO.builder()
                .salesList(statisticsList)
                .dateList(dateList)
                .build();
    }

}
